import 'package:flutter/material.dart';
import 'package:x3_component_core/models/base_custom_checkbox_tile_model.dart';

class BaseCustomCheckboxListTile extends StatefulWidget {
  final BaseCustomCheckboxListTileModel item;
  final Widget Function(BaseCustomCheckboxListTileModel) widgetDecorator;
  final Function(bool) onChange;
  const BaseCustomCheckboxListTile({Key key, @required this.item, this.onChange, this.widgetDecorator})
      : assert(item != null),
        super(key: key);
  @override
  _BaseCustomCheckboxListTileState createState() => _BaseCustomCheckboxListTileState();
}

class _BaseCustomCheckboxListTileState extends State<BaseCustomCheckboxListTile> {
  @override
  Widget build(BuildContext context) {
    return CheckboxListTile(
      dense: true,
      tristate: false,
      contentPadding: EdgeInsets.only(left: 4),
      controlAffinity: ListTileControlAffinity.leading,
      value: widget.item.isChecked,
      title: _buildTitle(),
      onChanged: (v) {
        setState(() {
          widget.item.isChecked = v;
        });
        if (widget.onChange != null) widget.onChange(v);
      },
    );
  }

  Widget _buildTitle() {
    if (widget.widgetDecorator != null) {
      return widget.widgetDecorator(widget.item);
    } else {
      return Text(widget.item.title ?? '', style: Theme.of(context).textTheme.bodyText2);
    }
  }
}
